package string;

import collection.UtilCollection;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


public class UtilString extends StringUtils{
	/** 7位ASCII字符，也叫作ISO646-US、Unicode字符集的基本拉丁块      */
	public static final String US_ASCII = "US-ASCII";
	/** ISO拉丁字母表 No.1，也叫做ISO-LATIN-1     */
	public static final String ISO_8859_1 = "ISO-8859-1";
	/** 8 位 UCS 转换格式     */
	public static final String UTF_8 = "UTF-8";
	/** 16 位 UCS 转换格式，Big Endian(最低地址存放高位字节）字节顺序     */
	public static final String UTF_16BE = "UTF-16BE";
	/** 16 位 UCS 转换格式，Litter Endian（最高地址存放地位字节）字节顺序     */
	public static final String UTF_16LE = "UTF-16LE";
	/** 16 位 UCS 转换格式，字节顺序由可选的字节顺序标记来标识     */
	public static final String UTF_16 = "UTF-16";
	/** 中文超大字符集     **/
	public static final String GBK = "GBK";

	public static final String GB2312 = "GB2312";

	public static  String left(String s,int number){
		if(number<=0){
			throw  new RuntimeException("error : number < = 0");
		}
		if (UtilString.isNullOrEmpty(s)) {
			return s;
		}
		int length=s.length();
		if(number>=length){
			return s;
		}
		return s.substring(0,number);
	}

	public static  String right(String s,int number){
		if(number<=0){
			throw  new RuntimeException("error : number < = 0");
		}
		if (UtilString.isNullOrEmpty(s)) {
			return s;
		}
		int length=s.length();
		if(number>=length){
			return s;
		}
		return s.substring(length-number,length);
	}

	public static boolean compareIgnoreCaseAndSpace(String w1,String w2){
		if(w1==null || w2==null){
			throw  new NullPointerException("At least one of parameters is null.");
		}
		w1=w1.trim();
		w2=w2.trim();
		return w1.compareToIgnoreCase(w2)==0;
	}

	public static void toLowerCase(Collection<String> collection){
		List<String> copy=new ArrayList(collection.size());
		copy.addAll(collection);
		collection.clear();
		for (String s: copy) {
			if (s != null) {
				s=s.toLowerCase();
			}
			collection.add(s);
		}
	}

	public static void toUpperCase(Collection<String> collection){
		List<String> copy=new ArrayList(collection.size());
		copy.addAll(collection);
		collection.clear();
		for (String s: copy) {
			if (s != null) {
				s=s.toUpperCase();
			}
			collection.add(s);
		}
	}

	public static boolean isNullOrEmpty(String s) {
		if(s==null)
			return true;
		if(s.trim().equals("")){
			return true;
		}
		return false;
	}

	/**
	 * @param array
	 * @return
	 */
	public static boolean isNullOrEmpty(String... array) {
		if(array==null){
			return true;
		}
		if(array.length==0){
			return true;
		}
		for(String s:array){
			if(isNullOrEmpty(s)){
				return true;
			}
		}
		return false;
	}

	    /**
	    * 根据length;删除字符串最后几位，返回截取后的字符串
	    * @param  s 待处理字符串
	    * @param  length 拟删除长度,必须大于0
	    * @return String     返回截取后的字符串
	    */
	public static String deleteTail(String s,int length){
		/*if(s==null){
			return null;
		}*/
		if(s.equals("")){
			return s;
		}
		if(length<0){
			throw new RuntimeException("p:length has error ;");
		}
		if(length>=s.length()){
			return "";
		}
		return s.substring(0, s.length()-length);
	}
	
	public static String deleteTail(StringBuffer sb,int length){
		return deleteTail(sb.toString(), length);
	}
	
	
	public static List<Long> string2LongList(String s,String dividor){
		List<Long> list=new ArrayList<>();
		if(!isNullOrEmpty(s)){
			String[] arrayString=s.split(dividor);
			for(String string:arrayString){
				Long l=Long.parseLong(string);
				list.add(l);
			}
		}
		return list;
	}
	
	public static List<Integer> string2IntegerList(String s,String dividor){
		List<Integer> list=new ArrayList<>();
		if(!isNullOrEmpty(s)){
			String[] arrayString=s.split(dividor);
			for(String string:arrayString){
				Integer l=Integer.parseInt(string);
				list.add(l);
			}
		}
		return list;
	}
	
	
	   /**
	    * @Title: getLastSubString
	    * @Description: 根据分隔符,返回最后一个匹配字符后面个子字符串:如 123.x1.jpg   调用后返回 jpg;如 找不到匹配的或者分隔符位于最后一个字符位置,返回""
	    * @param @param source 待处理字符串
	    * @param @param dividor 分隔符
	    * @param @return    返回截取后的字符串
	    * @return String     返回截取后的字符串
	    */
	public static String getLastSubString(String source,String dividor){
		String result="";
		if(UtilString.isNullOrEmpty(source) || source.indexOf(dividor)<0){
			return source;
		}else{
			int lastPostion=source.lastIndexOf(dividor);
			int length=source.length();
			if(lastPostion>0 && lastPostion<length-1){
				result=source.substring(lastPostion+1, source.length());
			}
		}
		return result;
	}

	/**
	 * @param source
	 * @param token
	 * @return
	 */
	static public String surround(String source,String token){
		if (UtilString.isNullOrEmpty(source)) {
			return source;
		}
		return token+source+token;
	}

	static public String join(Object[] objects,String seporator){
		return org.apache.commons.lang3.StringUtils.join(objects,seporator);
	}

	static public String join(Collection collection,String seporator){
		if(collection==null){
			return null;
		}
		return org.apache.commons.lang3.StringUtils.join(collection.toArray(),seporator);
	}

	static public String joinWithQuote(Collection collection,String seporator,String quote){
		if(collection==null){
			return null;
		}
		List<String> list= UtilCollection.toStringList(collection);
		List<String> rList=new ArrayList<>(list.size());
		for (String s : list) {
			s=UtilString.surround(s,quote);
			rList.add(s);
		}
		return  join(rList,seporator);
	}

	static public String joinWithQuote(Object[] objects,String seporator,String quote){
		if(objects==null){
			return null;
		}
		List<String> list= UtilCollection.toStringList(objects);
		return  joinWithQuote(list,seporator,quote);
	}
}
